package com.itheima.service;

import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.constant.MessageConstant;
import com.itheima.dao.MemberDao;
import com.itheima.dao.OrderDao;
import com.itheima.dao.OrderSettingDao;
import com.itheima.dao.ReserveOrderDao;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryString;
import com.itheima.entity.Result;
import com.itheima.pojo.Member;
import com.itheima.pojo.Order;
import com.itheima.pojo.OrderSetting;
import com.itheima.pojo.ReserveOrder;
import com.itheima.utils.DateUtils;
import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service(interfaceClass = ReserveOrderService.class)
@Transactional
public class ReserveOrderServiceImpl implements ReserveOrderService {

    @Autowired
    private OrderSettingDao orderSettingDao;
    @Autowired
    private MemberDao memberDao;
    @Autowired
    private OrderDao orderDao;
    @Autowired
    private ReserveOrderDao reserveOrderDao;

    @Override
    public PageResult findPage(QueryString queryString) {
        Integer currentPage = queryString.getCurrentPage();
        Integer pageSize = queryString.getPageSize();
        //分页插件，会在执行sql之前将分页关键字追加到SQL后面
        PageHelper.startPage(currentPage,pageSize);
        Page<ReserveOrder> page = reserveOrderDao.findByCondition(queryString);

        return new PageResult(page.getTotal(),page.getResult());
    }

    @Override
    public Result add(Map map, Integer[] setmealIds) throws Exception {


        //获取日期
        String orderDate = (String) map.get("orderDate");
        Date date = DateUtils.parseString2Date(orderDate);
        OrderSetting orderSetting = orderSettingDao.findByOrderDate(date);//根据日期查询预约设置信息
        if(orderSetting == null){
            //所选日期没有提前进行预约设置，不能完成预约
            return new Result(false, MessageConstant.SELECTED_DATE_CANNOT_ORDER);
        }

        if(orderSetting.getReservations() >= orderSetting.getNumber()){
            //所选日期已经约满，无法预约
            return new Result(false, MessageConstant.ORDER_FULL);
        }
        //判断是否在重复预约
        String phoneNumber = (String) map.get("phoneNumber");
        Member member = memberDao.findByTelephone(phoneNumber);
        Order order = new Order();
        for (Integer setmealId : setmealIds) {
            order.setSetmealId(setmealId);
            if (member == null) {
                member = new Member();
                member.setRegTime(new Date());
                member.setPhoneNumber(phoneNumber);
                member.setName((String) map.get("memberName"));
                reserveOrderDao.addMember(member);

            } else {
                order.setMemberId(member.getId());
                order.setOrderDate(date);
                List<Order> orderList = orderDao.findByCondition(order);
                if (orderList != null && orderList.size() > 0) {//用户在重复预约
                    return new Result(false, MessageConstant.HAS_ORDERED);
                }
            }
            order.setMemberId(member.getId());
            order.setOrderDate(date);

            order.setOrderType("电话预约");
            order.setOrderStatus("未到诊");
            orderDao.add(order);
            orderSetting.setReservations(orderSetting.getReservations()  + 1);
            orderSettingDao.editReservationsByOrderDate(orderSetting);
        }

        return new Result(true,MessageConstant.ORDER_SUCCESS);
        }


    @Override
    public void delete(Integer id) {
        reserveOrderDao.delete(id);
    }

    //根据id查询，数据回显
    @Override
    public ReserveOrder findById(Integer id) {
        return reserveOrderDao.findById(id);
    }

    @Override
    public void edit(ReserveOrder reserveOrder) {
        Integer id = reserveOrder.getId();
        String memberName = reserveOrder.getMemberName();
        Date orderDate = reserveOrder.getOrderDate();
        String orderStatus = reserveOrder.getOrderStatus();
        String orderType = reserveOrder.getOrderType();
        Integer memberid = reserveOrderDao.findByIdAndMemberId(id);
        Map<String,Object> map = new HashMap();
        map.put("id",id);
        map.put("orderDate",orderDate);
        map.put("orderStatus",orderStatus);
        map.put("orderType",orderType);
        Map<String,Object> map1 = new HashMap();
        map1.put("id",memberid);
        map1.put("name",memberName);
        reserveOrderDao.editMember(map1);
        reserveOrderDao.editOrder(map);
    }

    @Override
    public void addOrderStatus(ReserveOrder reserveOrder) {
        reserveOrderDao.addOrderStatus(reserveOrder);
    }
}
